import { getuserchan, getallchanlist } from '@/api/chanles'

const state = {
  // 推荐频道
  channelistL: [],
  // 全部频道
  allchannellist: []
}

const mutations = {
  setchannelist(state, payload) {
    state.channelistL = payload
  },
  setallchannelist(state, payload) {
    state.allchannellist = payload
  },
  addchannel(state, payload) {
    state.channelistL.push(payload)
  },
  delchannel(state, payload) {
    // console.log(payload)
    state.channelistL = state.channelistL.filter((item) => item.id !== payload)
  }
}

const actions = {
  async actions_channelist(context) {
    const { data: res } = await getuserchan()
    // console.log(res.channels)
    context.commit('setchannelist', res.channels)
  },
  async actions_allchannerlist(context) {
    const { data: res } = await getallchanlist()
    // console.log(res.channels)
    context.commit('setallchannelist', res.channels)
  }
}
const getters = {
  optionchannerlist(state) {
    return state.allchannellist.filter(
      (item) => !state.channelistL.some((channel) => channel.id === item.id)
    )
  }
}
export default {
  state,
  mutations,
  getters,
  actions,
  namespaced: true
}
